# 打开文件

file = open("../data/students.txt", mode="r", encoding="UTF-8")
# 读取所有行，返回一个列表
lines = file.readlines()
# 去掉前后的制表符和空格
lines = [line.strip() for line in lines]
print(lines)
# 关闭文件
file.close()

# with语句,当with代码快执行完成后会自动关闭文件
with open("../data/students.txt", mode="r", encoding="UTF-8") as f:
    students = [line.strip() for line in f.readlines()]

print(students)

# 取出班级
# map方法
clazz = list(map(lambda stu: stu.split(",")[-1], students))
print(clazz)

# 取出性别为男的学生
# filter方法
student_nan = list(filter(lambda stu: stu.split(",")[-2] == "男", students))
print(student_nan)

# 报错结果到文件中

# w:覆盖写入，a:追加写入
with open("../data/student_nan.txt", mode="w", encoding="UTF-8") as f:
    for stu in student_nan:
        f.write(stu)
        f.write("\n")
